package com.cn.web.controller.admin;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.Button;
import com.cn.bean.pojo.bc.Menu;
import com.cn.bean.pojo.bc.Role;
import com.cn.bean.vo.bc.MenuRoleId;
import com.cn.bean.vo.bc.RoleMenuButton;
import com.cn.config.config.Msg;
import com.cn.service.server.admin.AdminButtonServer;
import com.cn.service.server.admin.AdminMenuServer;
import com.cn.service.server.admin.AdminRoleServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@CrossOrigin
@RestController
public class AdminRoleController {
    private final AdminRoleServer adminRoleServer;
    private final AdminMenuServer adminMenuServer;
    private final AdminButtonServer adminButtonServer;

    @Autowired
    public AdminRoleController(AdminRoleServer adminRoleServer, AdminMenuServer adminMenuServer, AdminButtonServer adminButtonServer) {
        this.adminRoleServer = adminRoleServer;
        this.adminMenuServer = adminMenuServer;
        this.adminButtonServer = adminButtonServer;
    }

    @RequestMapping("/select/Admin/Button/RoleId/MenuAll")
    @ResponseBody
    public RoleMenuButton selectAdminButtonRoleIdMenuAll(@RequestBody JSONObject jsonObject) {
        RoleMenuButton roleMenuButton = new RoleMenuButton();
        Integer roleId = jsonObject.getInteger("roleId");
        List<Button> buttons = adminButtonServer.selectAdminButtonRoleIdMenu(roleId);
        List<Button> buttons1 = adminButtonServer.selectAdminButtonRoleIdMenuAll(roleId);
        roleMenuButton.setButtonListMenu(buttons1);
        roleMenuButton.setButtonListRole(buttons);
        return roleMenuButton;
    }

    @RequestMapping("/select/Admin/Menu/RoleId")
    @ResponseBody
    public MenuRoleId selectAdminRoleIsRole(@RequestBody JSONObject jsonObject) {
        MenuRoleId menuRoleId = new MenuRoleId();
        Integer roleId = jsonObject.getInteger("roleId");
        List<Menu> menus = adminMenuServer.selectMenu();
        List<Menu> menus1 = adminMenuServer.selectMenuRoleId(roleId);
        menuRoleId.setMenusListAll(menus);
        menuRoleId.setMenusListRoleId(menus1);
        return menuRoleId;
    }

    @RequestMapping("/select/Menu/RoleId")
    @ResponseBody
    public List<Menu> selectMenuRoleId(@RequestBody JSONObject jsonObject) {
        List<Menu> menus = adminMenuServer.selectMenu();
        return menus;
    }
    @RequestMapping("/insert/Admin/Role/Menu/Button")
    @ResponseBody
    public Msg insertAdminRoleMenuButton(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        int roleId = jsonObject.getInteger("roleId");
        JSONArray menuIdArray = jsonObject.getJSONArray("menuIdArray");
        JSONArray buttonIdArray = jsonObject.getJSONArray("buttonIdArray");

        int i1 = adminButtonServer.deleteAdminRoleMenuButton(roleId);
        boolean isI = true;

        for (int i = 0; i < menuIdArray.size(); i++) {
            int menuId = menuIdArray.getInteger(i);
            int buttonId = buttonIdArray.getInteger(i);
            int i2 = adminButtonServer.insertAdminRoleMenuButton(roleId, menuId, buttonId);
            if (i2==0){
                isI =false;
            }
        }
        if (isI) {
            msg.setCode(200);
            msg.setMessage("保存成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("保存失败");
        return msg;
    }
    @RequestMapping("/Insert/Admin/RoleMenu")
    @ResponseBody
    public Msg InsertAdminRoleMenu(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        int roleId = jsonObject.getInteger("roleId");
        JSONArray roleIdArray = jsonObject.getJSONArray("roleIdArray");
        int i1 = adminRoleServer.deleteAdminRoleMenu(roleId);
        boolean isI = true;
        for (int i = 0; i < roleIdArray.size(); i++) {
            int menuId = roleIdArray.getInteger(i);
            int i2 = adminRoleServer.InsertAdminRoleMenu(roleId,menuId);
            if (i2==0){
                isI =false;
            }
        }

        if (isI) {
            msg.setCode(200);
            msg.setMessage("保存成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("保存失败");
        return msg;
    }


    @RequestMapping("/select/Admin/Role")
    @ResponseBody
    public List<Role> selectAdminRole(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String roleId = jsonObject.getString("roleId");
        String roleName = jsonObject.getString("roleName");
        roleId = (roleId != null) ? roleId : "";
        roleName = (roleName != null) ? roleName : "";
        List<Role> roles = adminRoleServer.selectAdminRole(roleId, roleName, limit, page);
        return roles;
    }

    @RequestMapping("/insert/Admin/Role")
    @ResponseBody
    public Msg insertAdminRole(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String roleName = jsonObject.getString("roleName");
        Integer status = jsonObject.getInteger("status");
        String remark = jsonObject.getString("remark");
        boolean isI = true;
        int roleNameCount = adminRoleServer.repeatRoleName(roleName);
        if (roleNameCount > 0) {
            msg.setCode(500);
            msg.setMessage("角色名称已存在，请重新输入");
            return msg;
        }
        int roleId = adminRoleServer.getAutoId() + 1;
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间
        String register_time = now.format(formatter);
        int i = adminRoleServer.insertAdminRole(roleId, roleName, status, remark, register_time);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("保存成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("保存失败");
        return msg;
    }

    @RequestMapping("/update/Admin/Role")
    @ResponseBody
    public Msg updateAdminRole(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String roleName = jsonObject.getString("roleName");
        Integer status = jsonObject.getInteger("status");
        String remark = jsonObject.getString("remark");
        Integer roleId = jsonObject.getInteger("roleId");
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间
        String update_time = now.format(formatter);
        List<Role> roles = adminRoleServer.selectRoleId(roleId);
        String roleName1 = roles.get(0).getRoleName();
        if (!roleName1.equals(roleName)) {
            int roleNameCount = adminRoleServer.repeatRoleName(roleName);
            if (roleNameCount > 0) {
                msg.setCode(500);
                msg.setMessage("角色名称已存在，请重新输入");
                return msg;
            }
        }
        int i = adminRoleServer.updateAdminRole(roleId, roleName, status, remark, update_time);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }

    @RequestMapping("/delete/Admin/Role")
    @ResponseBody
    public Msg deleteAdminRole(@RequestBody JSONObject jsonObject) {
        Integer roleId = jsonObject.getInteger("roleId");
        Msg msg = new Msg();
        int i = adminRoleServer.deleteAdminRole(roleId);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("删除成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("删除失败");
        return msg;
    }
}
